package dio.com.gui;

import dio.com.controller.UsuarioController;
import dio.com.model.bean.Usuario;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Principal extends javax.swing.JFrame {

    int conta = 0;
    String seta_look = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    String mac = "ch.randelshofer.quaqua.QuaquaLookAndFeel";

    /** Creates new form login */
    public Principal() {
        ImageIcon icone = new ImageIcon("img/bookclosed.gif");
        setIconImage(icone.getImage());
        initComponents();
        lookandfeel();
        getRootPane().setDefaultButton(jButton1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfLogin = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        tfSenha = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tela de Login ao Sistema de Controle de Movimentos");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Login");

        jLabel2.setText("Usuário.:");

        jLabel3.setText("Senha.:");

        jButton1.setText("Entrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Limpar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Diógenes Sistemas - Controle de Movimentos");

        jLabel5.setText("Contato: 08194063266 - diogenesr.freitas@gmail.com");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel4).addComponent(jLabel5))).addGroup(layout.createSequentialGroup().addGap(27, 27, 27).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel2).addComponent(jLabel3)).addGap(27, 27, 27).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(tfSenha).addComponent(tfLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE).addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)).addGap(11, 11, 11).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE).addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))).addGap(67, 67, 67)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(27, 27, 27).addComponent(jLabel1).addGap(32, 32, 32).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(tfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jButton1)).addGap(15, 15, 15).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3).addComponent(jButton2).addComponent(tfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE).addComponent(jLabel4).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel5).addGap(32, 32, 32)));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 400) / 2, (screenSize.height - 300) / 2, 400, 300);
    }// </editor-fold>                           
   
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        /*
        if (tfLogin.getText().equals("") || tfSenha.getPassword().equals("")) {
            JOptionPane.showMessageDialog(null, "Os campos não podem ser vazios");
        } else {

            if (conta <= 1) {
                try {
                    UsuarioController uc = new UsuarioController();
                    Usuario u = uc.validarUsuario(tfLogin.getText(), tfSenha.getText());
                    if (u != null) {
                        if (!u.isLogado()) {
                            TelaPrincipal tp = new TelaPrincipal(u);
                            tp.setVisible(true);
                            dispose();


                        } else {
                            JOptionPane.showMessageDialog(null, "Usuário logado em outro terminal");
                            jButton2.doClick();
                        }
                    } else {
                        conta++;
                        if (conta == 1) {
                            JOptionPane.showMessageDialog(null, "Senha Incorreta, Você só tem mais uma chance");
                        } else {
                            JOptionPane.showMessageDialog(null, "De novo senha incorreta, Usuário Inabilitado para usar o Sistema\n\nO Sistema será finalizado");
                            dispose();
                        }
                    }
                } catch (Exception erro) {
                    erro.printStackTrace();
                }
            }

        }*/

        Usuario u = new Usuario();
        u.setNome("testador");
        TelaPrincipal teste = new TelaPrincipal(u);
        teste.setVisible(true);
        dispose();     
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        tfLogin.setText("");
        tfSenha.setText("");
    }

    public void lookandfeel() {
        System.setProperty(
          "Quaqua.design","jaguar"
        );
        try {
            UIManager.setLookAndFeel(seta_look);
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, erro);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Principal().setVisible(true);
                //TelaPrincipal main = new TelaPrincipal();
                //main.setVisible(true);                
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField tfLogin;
    private javax.swing.JPasswordField tfSenha;
    // End of variables declaration                   
}
